<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\Request;
use think\Response;

class User extends Base
{
    /**
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Response
    {
        //
//        return \app\model\User::select();

        //标准api结构生成
//        $result = [
//            //状态码
//            'code'=>200,
//            //消息
//            'msg'=>'数据请求生成',
//            //数据
//            'data'=>\app\model\User::select()
//        ];
//        return Response::create($result,'json');


        //获取数据列表
        //$data = \app\model\User::field('id,username,gender,email')->select();
        //设置分页
        //方法1
//        $data = \app\model\User::field('id,username,gender,email')->paginate(5);
        //方法2
        $data  = \app\model\User::field('id,username,gender,email')->page($this->page,$this->page_size)->select();
        //判断是否有值
        if($data->isEmpty()){
            return $this->create([],'数据不存在',204);
        }else{
            return $this->create($data,'数据请求成功',200);
        }

    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     *
     * @return Response
     */
    public function save(Request $request)
    {
        //获取数据
        $data = $request->post();
        //验证返回
        try {
            //验证
           validate(\app\validate\User::class)->check($data);
        }catch (ValidateException $exception){
            return $this->create([],$exception->getError(),400);
        }

        $data['password'] = md5($data['password']);
        try {
            $id = \app\model\User::create($data)->getData('id');
            return $this->create($id,'注册成功',200);
        }catch (PDOException $PDOException){
            return $this->create([],'注册失败',400);
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function read($id)
    {

        //判断id是否正常
        if(!Validate::isInteger($id)){
            return $this->create([],'id参数错误~',400);
        }
        //获取数据
        $data = \app\model\User::field('id,username,gender,email')->find($id);
        //判断是否有值
        if(empty($data)){
            return $this->create([],'数据不存在',204);
        }else{
            return $this->create($data,'数据请求成功',200);
        }
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @param  int    $id
     *
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        try{
            validate(\app\validate\User::class)->scene('update')->check($data);
        }catch(ValidateException $validateException){
            return $this->create([],$validateException->getError(),400);
        }

        try{
            //获取数据库里的数据
            $updateData = \app\model\User::find($id);
            //邮箱修改时不可以一致
            if($updateData->email == $data['email']){
                return $this->create([],'修改的邮箱不可以一致',400);
            }
            $id = \app\model\User::update($data)->getData('id');
            if(empty($id)){
                return $this->create([],'修改失败~',400);
            }else{
                return $this->create($id,'修改成功',200);
            }
        }catch (\Exception $exception){
            return $this->create([],'数据不存在导致错误！',400);
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function delete($id)
    {
        //
        if(!Validate::isInteger($id)){
            return $this->create([],'id参数错误~',400);
        }
        try{
            $user = \app\model\User::find($id);
            $user->delete();
            return $this->create([],'数据删除成功',200);
        }catch(\Error $error){
            return $this->create([],'无法删除或数据不存在导致错误！',400);
        }


    }

    public function book($id){
        if(!Validate::isInteger($id)){
            return $this->create([],'id参数不对~',400);
        }

        if( $user = \app\model\User::find($id)){
            $data  = $user->book;
            if($data->isEmpty()){
                return $this->create([],'数据不存在(无爱好)！',204);
            }else{
                return $this->create($data,'数据请求成功',200);
            }
        }else{
            return $this->create([],'查无此人！',400);
        };
    }

    public function login(Request $request){
        $data = $request->param();
        //验证用户密码
        $result = Validate::rule([
            'username'=> 'unique:user,username^password'
        ])->check(
            [
                'username'=>$data['username'],
                'password'=>$data['password']
            ]
        );

        //判断 反向
        if(!$result){
            session('admin',$data['username']);
            return $this->create(true,"登录成功",400);
        }else{
            return $this->create([],'用户名或密码错误',400);
        }
    }
}
